package com.ctshk.gateway.handler;

import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;

/**
 * @author 谢诗宏
 * @description 拦截gateway网关层异常定义
 * @date 2020/12/10
 */
@Slf4j
public class GatewayExceptionHandler extends AbstractExceptionHandler implements ErrorWebExceptionHandler {

    private static final String TRACE_ID = "traceId";

    private static final String CTSHK_SERVICE_NOT_FOUND = "Unable to find instance for ";

    private static final String CTSHK_SERVICE_SENTINEL = "com.alibaba.csp.sentinel.slots.block.flow.FlowException";

    private static final String CTSHK_SERVICE_NOT_FOUND_ZH = "未找到%s微服务,请检查服务是否可用";

    private List<HttpMessageReader<?>> messageReaders = Collections.emptyList();

    private List<HttpMessageWriter<?>> messageWriters = Collections.emptyList();

    private List<ViewResolver> viewResolvers = Collections.emptyList();

    private ThreadLocal<Result> exceptionHandlerResult = new ThreadLocal<>();

    public void setMessageReaders(List<HttpMessageReader<?>> messageReaders) {
        Assert.notNull(messageReaders, "'messageReaders' must not be null");
        this.messageReaders = messageReaders;
    }

    public void setViewResolvers(List<ViewResolver> viewResolvers) {
        this.viewResolvers = viewResolvers;
    }

    public void setMessageWriters(List<HttpMessageWriter<?>> messageWriters) {
        Assert.notNull(messageWriters, "'messageWriters' must not be null");
        this.messageWriters = messageWriters;
    }

    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
        String errorMessage = super.formatMessage(ex);
        int code;
        String errDesc = null;
        try {
            errDesc = ex.getMessage();
            ResponseStatusException responseStatusException = (ResponseStatusException) ex;
            code = responseStatusException.getStatus().value();
        } catch (Exception e) {
            // 限流异常
            if (ex instanceof FlowException) {
                code = SystemError.SYS_407.getCode();
                errorMessage = SystemError.SYS_407.getMsg();
            } else {
                code = SystemError.SYS_500.getCode();
                errorMessage = SystemError.SYS_500.getMsg();
            }
        }

        if (errorMessage.indexOf(CTSHK_SERVICE_SENTINEL) != -1) {
            code = SystemError.SYS_407.getCode();
            errorMessage = SystemError.SYS_407.getMsg();
        }

        if (errorMessage.indexOf(CTSHK_SERVICE_NOT_FOUND) != -1) {
            // errorMessage = String.format(CTSHK_SERVICE_NOT_FOUND_ZH, errorMessage.replace(CTSHK_SERVICE_NOT_FOUND, ""));
            code = SystemError.SYS_502.getCode();
            errorMessage = SystemError.SYS_502.getMsg();
        }

        Result result = super.buildErrorMap(code, errorMessage, errDesc);
        ServerHttpRequest request = exchange.getRequest();
        String traceId = request.getHeaders().getFirst(TRACE_ID);
        log.error("GatewayExceptionHandler request info [traceId={}] result error=[{}]", traceId, JSONObject.toJSONString(result));
        if (exchange.getResponse().isCommitted()) {
            return Mono.error(ex);
        }
        exceptionHandlerResult.set(result);
        ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders);
        return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest)
                .switchIfEmpty(Mono.error(ex))
                .flatMap((handler) -> handler.handle(newRequest))
                .flatMap((response) -> write(exchange, response));
    }


    protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
        Result result = exceptionHandlerResult.get();
        return ServerResponse.status(HttpStatus.OK)
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .body(BodyInserters.fromObject(result));
    }


    private Mono<? extends Void> write(ServerWebExchange exchange,
                                       ServerResponse response) {
        exchange.getResponse().getHeaders()
                .setContentType(response.headers().getContentType());
        return response.writeTo(exchange, new ResponseContext());
    }

    private class ResponseContext implements ServerResponse.Context {

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return GatewayExceptionHandler.this.messageWriters;
        }

        @Override
        public List<ViewResolver> viewResolvers() {
            return GatewayExceptionHandler.this.viewResolvers;
        }

    }
}
